/**************************************************************************************

Replication Materials for Tables and Figures in:

"Community Income Inequality and the Economic Gap in Participation"

By James Szewczyk and Melody Crowder-Meyer

Political Behavior

**************************************************************************************/


********************************************
* Creating the variables needed for analysis
***********************


*********
* 2012 American National Election Study (ANES) - Restricted Data File
* Access to the ANES Restricted Data Files can be obtained following the instructions here: electionstudies.org/restricted-data-access
*********

* zipcode 
	* provided in the ANES Restricted Data File - RDA Geocodes

* mergedweight
	* From 2012 ANES, use weight_full
	
* year2016	
	gen year2016 = 0
	
* income 
	gen income = incgroup_prepost if (incgroup_prepost>0)

* education 
	gen education = dem_edugroup if (dem_edugroup>0)

* age 
	gen age = dem_agegrp_iwdate if (dem_agegrp_iwdate>0)
	
* female 
	gen female = 0 if gender_respondent==1
	replace female = 1 if gender_respondent==2

* black 
	gen black = 0 if dem_raceeth==1 | dem_raceeth==3 | dem_raceeth==4 | dem_raceeth==5 | dem_raceeth==6
	replace black = 1 if dem_raceeth==2

* hispanic 
	gen hispanic = 0 if dem_raceeth==1 | dem_raceeth==2 | dem_raceeth==3 | dem_raceeth==4 | dem_raceeth==6
	replace hispanic = 1 if dem_raceeth==5

* asian
	gen asian = 0 if dem_raceeth==1 | dem_raceeth==2 | dem_raceeth==4 | dem_raceeth==5 | dem_raceeth==6
	replace asian = 1 if dem_raceeth==3

* othermultirace 
	gen othermultirace = 0 if dem_raceeth==1 | dem_raceeth==2 | dem_raceeth==3 | dem_raceeth==5
	replace othermultirace = 1 if dem_raceeth==4 | dem_raceeth==6

* white (excluded category)
	gen white = 0 if dem_raceeth==2 | dem_raceeth==3 | dem_raceeth==4 | dem_raceeth==5 | dem_raceeth==6
	replace white = 1 if dem_raceeth==1 
	
* married 
	gen married = 1 if dem_marital==1 | dem_marital==2
	replace married = 0 if dem_marital==3 | dem_marital==4 | dem_marital==5 | dem_marital==6

* numchildren 
	gen numchildren = dem2_numchild if dem2_numchild!=-9

* unionhh 
	gen unionhh = 1 if dem_unionhh==1
	replace unionhh = 0 if dem_unionhh==2

* employed 
	gen employed = 1 if dem_empstatus_1digitfin_x==1
	replace employed = 0 if dem_empstatus_1digitfin_x!=1 & dem_empstatus_1digitfin_x!=-9 & dem_empstatus_1digitfin_x!=-8

* churchattendance 
	gen churchattendance = 0 if relig_church==2 | relig_churchoft==5
	replace churchattendance = 1 if relig_churchoft==4
	replace churchattendance = 2 if relig_churchoft==3
	replace churchattendance = 3 if relig_churchoft==2
	replace churchattendance = 4 if relig_churchoft==1 & relig_churchwk ==1
	replace churchattendance = 5 if relig_churchoft==1 & relig_churchwk ==2

* strengthpid 
	gen pid = pid_x if pid_x!=-2

	gen strengthpid = 1 if pid==3 | pid==4 | pid==5
	replace strengthpid = 2 if pid==2 | pid==6
	replace strengthpid = 3 if pid==1 | pid==7

* repub
	gen repub = 1 if pid_x==5 | pid_x==6 | pid_x==7
	replace repub=0 if pid_x==1 | pid_x==2 | pid_x==3 | pid_x==4

* indep 
	gen indep = 1 if pid_x==4
	replace indep =0 if pid_x==1 | pid_x==2 | pid_x==3 | pid_x==5 | pid_x==6 | pid_x==7

* dem (excluded category)
	gen dem = 1 if pid_x==1 | pid_x==2 | pid_x==3 
	replace dem =0 if pid_x==5 | pid_x==6 | pid_x==7| pid_x==4

* incomequintile
	gen incomequintiles = 1 if income<=6 & income!=. 
	replace incomequintiles = 2 if income<=12 & income>6
	replace incomequintiles = 3 if income<=17 & income>12
	replace incomequintiles = 4 if income<=22 & income>17
	replace incomequintiles = 5 if income!=. & income>22

* Participation Variables:
* Mobilize/Persuade
	gen rmob = 1 if mobilpo_rmob == 1
	replace rmob = 0 if mobilpo_rmob == 2
* Discuss politics
	gen discuss = 1 if discuss_disc == 1
	replace discuss = 0 if discuss_disc == 2
* Petition on paper (2012)
	gen petition = 1 if dhsinvolv_petition == 1
	replace petition = 0 if dhsinvolv_petition == 2
* Petition online (2012)
	gen netpetition = 1 if dhsinvolv_netpetition == 1
	replace netpetition = 0 if dhsinvolv_netpetition == 2
* FB/Twitter message
	gen fbmessage = 1 if dhsinvolv_message == 1
	replace fbmessage = 0 if dhsinvolv_message == 2
* Button/Sign/Sticker
	gen buttonsign = 1 if mobilpo_sign == 1
	replace buttonsign = 0 if mobilpo_sign == 2
* Rallies
	gen rallies = 1 if mobilpo_rally == 1
	replace rallies = 0 if mobilpo_rally == 2
* Work for Party/Cand
	gen workptycand = 1 if mobilpo_otherwork == 1
	replace workptycand = 0 if mobilpo_otherwork == 2
* Contactgov
	gen contactgov = 1 if dhsinvolv_contact1 == 1
	replace contactgov = 0 if dhsinvolv_contact1 == 2
* Contribute to party/campaign/other
	gen givecan = mobilpo_ctbcand if mobilpo_ctbcand>0
	gen giveparty = mobilpo_ctbpty if mobilpo_ctbpty>0
	gen giveother = mobilpo_ctboth if mobilpo_ctboth>0
	gen giveany = 1 if givecan==1 | giveparty==1 | giveother==1
	replace giveany = 0 if givecan==2 & giveparty==2 & giveother==2 
* Vote
	gen vote = rvote2012_x if rvote2012_x!=-2 & rvote2012_x!=-6 & rvote2012_x!=-9 
	replace vote = 0 if vote==2

* Participation Scale
	gen particscale = rmob + discuss + petition + netpetition + fbmessage + buttonsign + rallies + workptycand + contactgov + giveany + vote


*********
* American Community Survey (ACS) 5 year estimates - 2008-2012
* Should be merged into 2012 ANES Restricted Data File
* ACS data can be downloaded from www.census.gov or specific variables needed are available in replication data files
* Download by ZCTA5 to merge with ANES Restricted Data by zip code
*********

* gini_zip 
	* ACS variable: HD01_VD01 "Estimate; Gini Index"
	* In Replication Data file "Gini - ACS_2012_5YR_B19083_with_ann.csv" 

* inc_mean_house_inthous 
	* ACS variable: HC01_EST_VC15 "Households; Estimate; Mean income (dollars)"
	* In Replication Data file "IncomeMean_ACS_2012_5YR_S1901_with_ann.csv" 
	
* racecomp_pct_hisp 
	* ACS variable: HC03_VC82 "Percent; HISPANIC OR LATINO AND RACE - Hispanic or Latino (of any race)
	* In Replication Data file "RacialComp_ACS_2012_5YR_DP05_with_ann.csv" 
	
* racecomp_pct_black 
	* ACS variable: HC03_VC89 "Percent; HISPANIC OR LATINO AND RACE - Not Hispanic or Latino - Black or African American alone"
	* In Replication Data file "RacialComp_ACS_2012_5YR_DP05_with_ann.csv" 


*********
* 2016 American National Election Study (ANES) - Restricted Data File
* Access to the ANES Restricted Data Files can be obtained following the instructions here: electionstudies.org/restricted-data-access
*********

* zipcode 
	* provided in the ANES Restricted Data File - RDA Geocodes

* mergedweight
	* From 2016 ANES, use V160102

* year2016	
	gen year2016 = 1

* income 
	gen income = V161361x if V161361x>0

* education 
	gen education = 1 if V161270>0 & V161270<=8
	replace education = 2 if V161270==9
	replace education = 3 if V161270>9 & V161270<=12
	replace education = 4 if V161270==13
	replace education = 5 if V161270>13 & V161270<90

* age 
	gen age = V161267  if (V161267 >0)

* female 
	gen female = 0 if V161342==1
	replace female = 1 if V161342==2

* black 
	gen black = 0 if V161310x==1 | V161310x==3 | V161310x==4 | V161310x==5 | V161310x==6
	replace black = 1 if V161310x==2

* hispanic 
	gen hispanic = 0 if V161310x==1 | V161310x==2 | V161310x==3 | V161310x==4 | V161310x==6
	replace hispanic = 1 if V161310x==5

* asian 
	gen asian = 0 if V161310x==1 | V161310x==2 | V161310x==4 | V161310x==5 | V161310x==6
	replace asian = 1 if V161310x==3

* othermultirace 
	gen othermultirace = 0 if V161310x==1 | V161310x==2 | V161310x==3 | V161310x==5
	replace othermultirace = 1 if V161310x==4 | V161310x==6

* white (excluded category)
	gen white = 0 if V161310x ==2 | V161310x==3 | V161310x==4 | V161310x==5 | V161310x==6
	replace white = 1 if V161310x==1 

* married 
	gen married = 1 if V161268==1 | V161268==2
	replace married = 0 if V161268==3 | V161268==4 | V161268==5 | V161268==6

* numchildren 
	gen numchildren = V161324  if V161324!=-9
	label var numchildren "number of children in HH: 0-8, 9=more than 8 kids"

* unionhh 
	gen unionhh = 1 if V161302 ==1
	replace unionhh = 0 if V161302 ==2

* employed 
	gen employed = 1 if V161277 ==1
	replace employed = 0 if V161277!=1 & V161277!=-9 

* churchattendance 
	gen churchattendance = 0 if V161244==2 | V161245==5
	replace churchattendance = 1 if V161245==4
	replace churchattendance = 2 if V161245==3
	replace churchattendance = 3 if V161245==2
	replace churchattendance = 4 if V161245==1 & V161245a ==1
	replace churchattendance = 5 if V161245==1 & V161245a ==2

* strengthpid 
	gen pid = V161158x  if V161158x >0 & V161158x~=.

	gen strengthpid = 1 if pid==3 | pid==4 | pid==5
	replace strengthpid = 2 if pid==2 | pid==6
	replace strengthpid = 3 if pid==1 | pid==7

* repub
	gen repub = 1 if V161158x ==5 | V161158x ==6 | V161158x ==7
	replace repub=0 if V161158x ==1 | V161158x ==2 | V161158x ==3 | V161158x ==4

* indep 
	gen indep = 1 if V161158x==4
	replace indep =0 if V161158x==1 | V161158x==2 | V161158x==3 | V161158x==5 | V161158x==6 | V161158x==7

* dem (excluded category)
	gen dem = 1 if V161158x==1 | V161158x==2 | V161158x==3 
	replace dem =0 if V161158x==5 | V161158x==6 | V161158x==7| V161158x==4

* incomequintile
	gen incomequintiles = 1 if income<=7 & income>0
	replace incomequintiles = 2 if income<=13 & income>7
	replace incomequintiles = 3 if income<=19 & income>13 
	replace incomequintiles = 4 if income<=23 & income>19
	replace incomequintiles = 5 if income!=. & income>23

* Participation Variables:
* Mobilize/Persuade
	gen rmob = 1 if V162010 == 1
	replace rmob = 0 if V162010 == 2
* Discuss politics
	gen discuss = 1 if V162174 == 1
	replace discuss = 0 if V162174 == 2
* Petition netorpaper (2016) 
	gen petition_netorpaper = 1 if V162018b == 1
	replace petition_netorpaper = 0 if V162018b == 2
* FB/Twitter message
	gen fbmessage = 1 if V162018e == 1
	replace fbmessage = 0 if V162018e == 2
* Button/Sign/Sticker
	gen buttonsign = 1 if V162012 == 1
	replace buttonsign = 0 if V162012 == 2
* Rallies
	gen rallies = 1 if V162011 == 1
	replace rallies = 0 if V162011 == 2
* Protest (2016) 
	gen protest = 1 if V162018a == 1
	replace protest = 0 if V162018a == 2
* Work for Party/Cand
	gen workptycand = 1 if V162013 == 1
	replace workptycand = 0 if V162013 == 2
* Contactgov
	gen contactgov = 1 if V162019 == 1
	replace contactgov = 0 if V162019 == 2
* Contribute to party/campaign/other
	gen givecan = V162014 if V162014>0
	gen giveparty = V162016 if V162016>0
	gen giveother = V162017 if V162017>0
	gen giveany = 1 if givecan==1 | giveparty==1 | giveother==1
	replace giveany = 0 if givecan==2 & giveparty==2 & giveother==2 
* Vote
	generate vote = 1 if V162031x==1 
	replace vote = 0 if V162031x==0 
	replace vote = 1 if V161022==1 
	replace vote = 0 if (V161011==3 & V162022==3) 

* Participation Scale 
	gen particscale = rmob + discuss + petition_netorpaper + fbmessage + buttonsign + rallies + protest + workptycand + contactgov + giveany + vote

	
*********
* American Community Survey (ACS) 5 year estimates - 2012-2016
* Should be merged into 2016 ANES Restricted Data File
* ACS data can be downloaded from www.census.gov or specific variables needed are available in replication data files
* Download by ZCTA5 to merge with ANES Restricted Data by zip code
*********

* gini_zip 
	* ACS variable: HD01_VD01 "Estimate; Gini Index"
	* In Replication Data file "Gini - ACS_2016_5YR_B19083_with_ann.csv"

* inc_mean_house_inthous 
	* ACS variable: HC01_EST_VC15 "Households; Estimate; Mean income (dollars)"
	* In Replication Data file "IncomeMean_ACS_2016_5YR_S1901_with_ann.csv"

* racecomp_pct_hisp 
	* ACS variable: HC03_VC88 "Percent; HISPANIC OR LATINO AND RACE - Hispanic or Latino (of any race)
	* In Replication Data file "RacialComp_ACS_2016_5YR_DP05_with_ann.csv"

* racecomp_pct_black 
	* ACS variable: HC03_VC95 "Percent; HISPANIC OR LATINO AND RACE - Not Hispanic or Latino - Black or African American alone"
	* In Replication Data file "RacialComp_ACS_2016_5YR_DP05_with_ann.csv"

	

*******************************************
* Code for Analyses 
***********************

* To prepare data for analysis, must first append the ANES 2012 and ANES 2016 Restricted Data Files 
	use [ANES 2016 data.dta]
	append using [ANES 2012 data.dta]
	save [new pooled dataset.dta]

* Then, using [new pooled dataset.dta]: 

* Table 1
	svyset zipcode [pweight=mergedweight], strata(year2016)	
	svy: regress particscale c.gini_zip income education c.age female i.black i.hispanic i.asian i.othermultirace married numchildren unionhh employed churchattendance strengthpid i.repub i.indep inc_mean_house_inthous racecomp_pct_hisp racecomp_pct_black year2016 if incomequintile==1
	svy: regress particscale c.gini_zip income education c.age female i.black i.hispanic i.asian i.othermultirace married numchildren unionhh employed churchattendance strengthpid i.repub i.indep inc_mean_house_inthous racecomp_pct_hisp racecomp_pct_black year2016 if incomequintile==2
	svy: regress particscale c.gini_zip income education c.age female i.black i.hispanic i.asian i.othermultirace married numchildren unionhh employed churchattendance strengthpid i.repub i.indep inc_mean_house_inthous racecomp_pct_hisp racecomp_pct_black year2016 if incomequintile==3
	svy: regress particscale c.gini_zip income education c.age female i.black i.hispanic i.asian i.othermultirace married numchildren unionhh employed churchattendance strengthpid i.repub i.indep inc_mean_house_inthous racecomp_pct_hisp racecomp_pct_black year2016 if incomequintile==4
	svy: regress particscale c.gini_zip income education c.age female i.black i.hispanic i.asian i.othermultirace married numchildren unionhh employed churchattendance strengthpid i.repub i.indep inc_mean_house_inthous racecomp_pct_hisp racecomp_pct_black year2016 if incomequintile==5
	
* Figure 1
	svyset zipcode [pweight=mergedweight], strata(year2016)	
	svy: regress particscale c.gini_zip income education c.age female i.black i.hispanic i.asian i.othermultirace married numchildren unionhh employed churchattendance strengthpid i.repub i.indep income_mean_house racecomp_pct_hisp racecomp_pct_black year2016  if incomequintile==1 
	margins, at(gini_zip=.2544)
	margins, at(gini_zip=.6879)

	svy: regress particscale c.gini_zip income education c.age female i.black i.hispanic i.asian i.othermultirace married numchildren unionhh employed churchattendance strengthpid i.repub i.indep income_mean_house racecomp_pct_hisp racecomp_pct_black year2016  if incomequintile==2
	margins, at(gini_zip=.2544)
	margins, at(gini_zip=.6879)

	svy: regress particscale c.gini_zip income education c.age female i.black i.hispanic i.asian i.othermultirace married numchildren unionhh employed churchattendance strengthpid i.repub i.indep income_mean_house racecomp_pct_hisp racecomp_pct_black year2016  if incomequintile==3 
	margins, at(gini_zip=.2544)
	margins, at(gini_zip=.6879)

	svy: regress particscale c.gini_zip income education c.age female i.black i.hispanic i.asian i.othermultirace married numchildren unionhh employed churchattendance strengthpid i.repub i.indep income_mean_house racecomp_pct_hisp racecomp_pct_black year2016  if incomequintile==4 
	margins, at(gini_zip=.2544)
	margins, at(gini_zip=.6879)

	svy: regress particscale c.gini_zip income education c.age female i.black i.hispanic i.asian i.othermultirace married numchildren unionhh employed churchattendance strengthpid i.repub i.indep income_mean_house racecomp_pct_hisp racecomp_pct_black year2016  if incomequintile==5 
	margins, at(gini_zip=.2544)
	margins, at(gini_zip=.6879)

* Figure 2 (individual forms of participation) 
	* [Participation Variable] in the code below should be replaced with each variable indicating individual forms of participation listed above (e.g., rmob, discuss, contactgov, vote) 
	svyset zipcode [pweight=mergedweight], strata(year2016)	
	svy: logit [Participation Variable] c.gini_zip income education c.age female i.black i.hispanic i.asian i.othermultirace married numchildren unionhh employed churchattendance strengthpid i.repub i.indep inc_mean_house_inthous racecomp_pct_hisp racecomp_pct_black  if incomequintile==1 
	svy: logit [Participation Variable] c.gini_zip income education c.age female i.black i.hispanic i.asian i.othermultirace married numchildren unionhh employed churchattendance strengthpid i.repub i.indep inc_mean_house_inthous racecomp_pct_hisp racecomp_pct_black  if incomequintile==2 
	svy: logit [Participation Variable] c.gini_zip income education c.age female i.black i.hispanic i.asian i.othermultirace married numchildren unionhh employed churchattendance strengthpid i.repub i.indep inc_mean_house_inthous racecomp_pct_hisp racecomp_pct_black  if incomequintile==3 
	svy: logit [Participation Variable] c.gini_zip income education c.age female i.black i.hispanic i.asian i.othermultirace married numchildren unionhh employed churchattendance strengthpid i.repub i.indep inc_mean_house_inthous racecomp_pct_hisp racecomp_pct_black  if incomequintile==4 
	svy: logit [Participation Variable] c.gini_zip income education c.age female i.black i.hispanic i.asian i.othermultirace married numchildren unionhh employed churchattendance strengthpid i.repub i.indep inc_mean_house_inthous racecomp_pct_hisp racecomp_pct_black  if incomequintile==5 

* Instrumental Variable Analysis 
	ivreg2 particscale  income education c.age female i.black i.hispanic i.asian i.othermultirace married numchildren unionhh employed churchattendance strengthpid i.repub i.indep income_mean_house racecomp_pct_hisp racecomp_pct_black year2016  (gini_zip = predicted_gini)   if incomequintile==1 [pw=mergedweight], cluster(zipcode) first
	ivreg2 particscale  income education c.age female i.black i.hispanic i.asian i.othermultirace married numchildren unionhh employed churchattendance strengthpid i.repub i.indep income_mean_house racecomp_pct_hisp racecomp_pct_black year2016  (gini_zip = predicted_gini)   if incomequintile==2 [pw=mergedweight], cluster(zipcode) first
	ivreg2 particscale  income education c.age female i.black i.hispanic i.asian i.othermultirace married numchildren unionhh employed churchattendance strengthpid i.repub i.indep income_mean_house racecomp_pct_hisp racecomp_pct_black year2016  (gini_zip = predicted_gini)   if incomequintile==3 [pw=mergedweight], cluster(zipcode) first
	ivreg2 particscale  income education c.age female i.black i.hispanic i.asian i.othermultirace married numchildren unionhh employed churchattendance strengthpid i.repub i.indep income_mean_house racecomp_pct_hisp racecomp_pct_black year2016  (gini_zip = predicted_gini)   if incomequintile==4 [pw=mergedweight], cluster(zipcode) first
	ivreg2 particscale  income education c.age female i.black i.hispanic i.asian i.othermultirace married numchildren unionhh employed churchattendance strengthpid i.repub i.indep income_mean_house racecomp_pct_hisp racecomp_pct_black year2016  (gini_zip = predicted_gini)   if incomequintile==5 [pw=mergedweight], cluster(zipcode) first

